package com.zs.toolbox.web2j.system.core.controller.common;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zs.toolbox.web2j.common.request.WtPageBodyModel;
import com.zs.toolbox.web2j.common.request.WtPageQuerySearchModel;
import com.zs.toolbox.web2j.common.request.WtPrimaryKeyReqeust;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtAuthContext;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.common.web.WtCommonPropertiesSetter;
import com.zs.toolbox.web2j.system.core.entity.WtPostDetailEntity;
import com.zs.toolbox.web2j.system.core.request.WtPostContentRequest;
import com.zs.toolbox.web2j.system.core.response.WtPostDetailWithUserInfoEntity;
import com.zs.toolbox.web2j.system.core.service.WtPostDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.zs.toolbox.web2j.common.exception.WtResponses.SUCCESS;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author eric.zhang
 * @since 2018-10-11
 */
@RequestMapping("/api/v1/admin/post/content")
@Api(description = "发布内容管理")
@Slf4j
@RestController
public class WtCommonPostContentController extends WtBaseController {
    @Autowired
    WtPostDetailService wtPostDetailService;

    @PostMapping("/savePost")
    @ApiOperation("保存文章内容")
    @PreAuthorize("isAuthenticated()")
    public WtBaseResponse<String> savePost(@RequestBody WtPostContentRequest wtPostContentRequest) {
        this.volidate(wtPostContentRequest);
        WtPostDetailEntity wtPostDetailEntity = new WtPostDetailEntity();
        WtCommonPropertiesSetter.setDefaultCommonProperty(wtPostDetailEntity);
        BeanUtils.copyProperties(wtPostContentRequest,wtPostDetailEntity);
        wtPostDetailEntity.setUserId(WtAuthContext.getUserId());
        wtPostDetailService.savePost(wtPostDetailEntity);
        return SUCCESS;
    }

    @RequestMapping("/detail")
    @ApiOperation("查看文章内容")
    public WtBaseResponse<WtPostDetailWithUserInfoEntity> postDetail(WtPrimaryKeyReqeust<Long> reqeust) {
        this.volidate(reqeust);
        WtPostDetailWithUserInfoEntity wtPostDetailWithUserInfoEntity = wtPostDetailService.fetchPostDetailByIdAndRead(reqeust.getId());
        return new WtBaseResponse<>(wtPostDetailWithUserInfoEntity);
    }

    @RequestMapping("/page")
    @ApiOperation("分页查看文章内容")
    public WtBaseResponse<WtPageBodyModel<WtPostDetailWithUserInfoEntity>> pagePost(@RequestBody WtPageQuerySearchModel wtPageQueryModel) {
        this.volidate(wtPageQueryModel);
        WtPageBodyModel<WtPostDetailWithUserInfoEntity> page = wtPostDetailService.pagePost(wtPageQueryModel);
        return new WtBaseResponse(page);
    }

    @RequestMapping("/delete")
    @ApiOperation("删除文章")
    public WtBaseResponse delete(WtPrimaryKeyReqeust<Long> reqeust) {
        this.volidate(reqeust);
        wtPostDetailService.remove(new LambdaQueryWrapper<WtPostDetailEntity>()
                .eq(WtPostDetailEntity::getPostId, reqeust.getId()).eq(WtPostDetailEntity::getUserId
                        , WtAuthContext.getUserId()));
        return SUCCESS;
    }

}
